package com.jushu.storbox.view.resolve;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import com.jushu.storbox.ActionShareActivity;
import com.jushu.storbox.R;
import com.jushu.storbox.actions.ActionShare;
import com.jushu.storbox.actions.FileAction;
import com.jushu.storbox.actions.FolderAction;
import com.jushu.storbox.adapter.MySendShareDisplayAdapter;
import com.jushu.storbox.callback.ShareNodeTypeSort;
import com.jushu.storbox.constant.ClientConfig;
import com.jushu.storbox.constant.Constant;
import com.jushu.storbox.constant.IntentConstant;
import com.jushu.storbox.constant.MessageCode;
import com.jushu.storbox.constant.UiConstant;
import com.jushu.storbox.entities.FileFolderInfo;
import com.jushu.storbox.entities.FileType;
import com.jushu.storbox.handler.ClientExceptionRelateHandler;
import com.jushu.storbox.operation.group.MySendShareOperation;
import com.jushu.storbox.util.LogUtil;
import com.jushu.storbox.view.dialog.AbsPopupMenuDialog;
import com.jushu.storbox.vo.PagedList;
import com.huawei.sharedrive.sdk.android.exception.ClientException;
import com.huawei.sharedrive.sdk.android.modelV2.request.SharedUserV2;
import com.huawei.sharedrive.sdk.android.modelV2.response.INodeShareV2;
import com.kennyc.view.MultiStateView;

public class MySendShareDisplayResolve
		extends MyShareDisplayResolve<INodeShareV2, MySendShareOperation<INodeShareV2>, MySendShareDisplayAdapter> {
	protected static final String TAG = MySendShareDisplayResolve.class.getSimpleName();

	private String clickedItemOwnerId = null;
	private int pageNum = 0;
	List<INodeShareV2> datas = new ArrayList<INodeShareV2>();
	private MultiStateView multiStateView;

	private OnClickListener listener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			multiStateView.setViewState(MultiStateView.ViewState.LOADING);
			loadDataList();
		}
	};

	public MySendShareDisplayResolve(Context context) {
		super(context);
	}
	
	public MySendShareDisplayResolve(Context context, MultiStateView multiStateView) {
		super(context);
		this.multiStateView = multiStateView;
		multiStateView.getView(MultiStateView.ViewState.ERROR).findViewById(R.id.button1).setOnClickListener(listener);
	}

	@Override
	protected MySendShareDisplayAdapter initTemplateAdapter() {
		return new MySendShareDisplayAdapter(context, datas);
	}

	@Override
	protected MySendShareOperation<INodeShareV2> initAdapterOperation() {
		return operation;
	}

	MySendShareOperation<INodeShareV2> operation = new MySendShareOperation<INodeShareV2>() {
		@Override
		public void onCancelShareItem(int index, final INodeShareV2 info) {
			AbsPopupMenuDialog cancelDialog = new AbsPopupMenuDialog(context, R.style.dialog_upload,
					R.layout.dialog_filedelete) {
				public void onInflaterFinished(View conventView) {
					Button dialog_cancal = (Button) conventView.findViewById(R.id.dialog_add_file_cancal_bt);
					Button dialog_OK = (Button) conventView.findViewById(R.id.dialog_add_file_ok_bt);
					TextView dialog_prompt_tv = (TextView) conventView.findViewById(R.id.dialog_prompt_tv);
					dialog_prompt_tv.setText(context.getString(R.string.action_share_promit_cancel_share));
					dialog_cancal.setOnClickListener(new View.OnClickListener() {
						@Override
						public void onClick(View v) {
							dismiss();
						}
					});
					dialog_OK.setOnClickListener(new View.OnClickListener() {
						@Override
						public void onClick(View v) {
							dismiss();
							SharedUserV2 sharedUser = new SharedUserV2();
							sharedUser.setId(null);
							sharedUser.setType(null);
							new ActionShare().deleteShareShips(context, sharedUser, info, handler);
						}
					});
				}
			};
			cancelDialog.show();
		}

		@Override
		public void onViewShareItem(int index, INodeShareV2 info) {
			if (info.getType() == FileType.File.value()) {
				new FileAction().getNodeInfomationFromServer(context, info, handler);
			} else if (info.getType() == FileType.Folder.value()) {
				new FolderAction().getNodeInfomationFromServer(context, info.getOwnerId().toString(),
						info.getNodeId().toString(), handler);
			} else {
				LogUtil.e(TAG, "unknow type");
			}

		}
	};

	protected void openFolder(String ownerId, PagedList<FileFolderInfo> pageList) {
	}

	@Override
	protected void getMoreDataList() {
		new ActionShare().getSharedResourceByMe(context, pageNum * Constant.MAX_SHARE_COUNT, Constant.MAX_SHARE_COUNT,
				handler);
	}

	protected void loadDataList() {

		new ActionShare().getSharedResourceByMe(context, 0, Constant.MAX_SHARE_COUNT, handler);
	}

	@Override
	protected void clickItem(View clickView, int position, long id, INodeShareV2 data) {
		clickedItemOwnerId = data.getOwnerId().toString();
		if (data.getType() == FileType.File.value()) {
			new ActionShare().openMyShareINodeFile(context, data, handler);
		} else if (data.getType() == FileType.Folder.value()) {
			new FolderAction().openFolder(context, MessageCode.OPEN_FOLDER, data.getOwnerId().toString(),
					data.getNodeId().toString(), data.getName(), Constant.FIRST_PAGE_INDEX, handler);
		} else {
		}
		// if(data.getType() == FileType.File.value()) {
		// new ActionShare().openMyShareINodeFile(context, data, handler);
		// } else if(data.getType() == FileType.File.value()) {
		// LogUtil.i(TAG, "open folder is not surppot!");
		//
		// }
	}

	@Override
	public void onActivityResult(int responseCode, Intent data) {
		switch (responseCode) {
		case IntentConstant.INTENT_RESPONSE_MYSHARE_UNKNOW:
			loadDataList();
			break;
		case IntentConstant.INTENT_RESPONSE_MYSHARE:
			break;
		case IntentConstant.INTENT_RESPONSE_MYSHARE_CANCELED:
			loadDataList();
			break;
		default:
			super.onActivityResult(responseCode, data);
		}
	}

	ClientExceptionRelateHandler handler = new ClientExceptionRelateHandler() {
		@SuppressWarnings("unchecked")
		@Override
		public void handleMessage(Message msg) {
			if (msg.obj instanceof ClientException && null != multiStateView) {
				multiStateView.setViewState(MultiStateView.ViewState.CONTENT);
			} else if (null != multiStateView) {
				multiStateView.setViewState(MultiStateView.ViewState.CONTENT);
			}
			switch (msg.what) {
			case MessageCode.OPEN_FOLDER:
				openFolder(clickedItemOwnerId, (PagedList<FileFolderInfo>) msg.obj);
				break;

			case UiConstant.SHARE_ACTION_GET_SHARE_RESOURCE_TOME:
				break;

			case UiConstant.SHARE_ACTION_GET_SHARE_RESOURCE_BYME:
				onRefreshComplete();
				// adapter.addDatas((List<INodeShareV2>) msg.obj);
				List<INodeShareV2> listdata = (List<INodeShareV2>) msg.obj;
				if (pageNum == 0) {
					adapter.cleanListDatasOnly();
				}
				adapter.addDatas(listdata);
				adapter.notifyDataSetChanged();
				if (listdata.size() > Constant.MAX_SHARE_COUNT) {
					pageNum++;
				}

				break;

			case UiConstant.FILE_ACTION_GET_NODE_INFOMATION:
				FileFolderInfo info = (FileFolderInfo) msg.obj;
				Intent intent = new Intent(context, ActionShareActivity.class);
				intent.putExtra(IntentConstant.INTENT_SHARE_DATA, info);
				// requestActivityForResult(intent);
				context.startActivity(intent);
				break;

			case UiConstant.SHARE_ACTION_DELETE_SHARE_SHIPS_SUCCESS:
				adapter.removeData((INodeShareV2) msg.obj);
				break;

			default:
				super.handleMessage(msg);
			}
		}

		@Override
		protected void onHandlerBefore(Message msg, String errorCode) {
			onRefreshComplete();
			super.onHandlerBefore(msg, errorCode);
		}

		@Override
		public Context getContext() {
			return context;
		}
	};

	public void shareSortByFileName(int sortType) {
		adapter.sort(new ShareNodeTypeSort(sortType));
		SharedPreferences preference = context.getSharedPreferences(ClientConfig.settings, Context.MODE_APPEND);
		Editor edit = preference.edit();
		edit.putInt(UiConstant.SHARE_SEND_CURRENT_SORT, sortType);
		edit.putInt(UiConstant.SHARE_SEND_FILENAME_SORT, sortType);
		edit.commit();
	}

}
